﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yaxunsoft.CasesManagement;
using FantastPlatform.Framework.IModel;

namespace FantastPlatform.Community.UI.Service
{
    public partial class CaseService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            switch (Action)
            {
                case "Search":
                    IList<IRole> roleList = LoginEmployee.User.Roles;
                    DateTime? startTime = getWeekStartTime();
                    List<ResponsibilityArea> ResponsibilityAreaList = new List<ResponsibilityArea>();
                    IOrganization org = LoginEmployee.EmployeePosts[0].Post.Organization;
                    getResponsibilityArea(org, ResponsibilityAreaList);

                    List<string> roles = new List<string>();

                    List<object> caseList = new List<object>();

                    foreach (IRole role in roleList)
                    {
                        roles.Add(role.Name);
                    }
                    if (roles.Contains("社区一级管理员") || roles.Contains("管理员"))
                    {
                        if (CaseType == "criminalCase")
                            caseList = getCriminalCase(null, startTime, DateTime.Now);
                        else if (CaseType == "securityCase")
                            caseList = getSecurityCase(null, startTime, DateTime.Now);
                    }
                    else
                    {
                        foreach (ResponsibilityArea area in ResponsibilityAreaList)
                        {
                            if (CaseType == "criminalCase")
                            {
                                List<object> criminalList = getCriminalCase(area.Id, startTime, DateTime.Now);
                                for (int i = 0; i < criminalList.Count; i++)
                                {
                                    caseList.Add(criminalList[i]);
                                }
                            }
                            else if (CaseType == "securityCase")
                            {
                                List<object> securityList = getSecurityCase(area.Id, startTime, DateTime.Now);
                                for (int i = 0; i < securityList.Count; i++)
                                {
                                    caseList.Add(securityList[i]);
                                }
                            }
                        }
                    }
                    ReturnList(caseList.Count, caseList);
                    break;
            }
        }

        private void getResponsibilityArea(IOrganization org, List<ResponsibilityArea> areaList)
        {
            if (org.Children.Count == 0)
            {
                List<ResponsibilityArea> area = ResponsibilityAreaManager.LoadList(org.Name);
                if (area.Count > 0)
                    areaList.Add(area[0]);
            }
            else
            {
                foreach (IOrganization area in org.Children)
                {
                    getResponsibilityArea(area, areaList);
                }
            }
        }

        private List<object> getCriminalCase(int? responsibilityAreaId, DateTime? startTime, DateTime? endTime)
        {
            CaseMainLineCondition condition = new CaseMainLineCondition();
            condition.ResponsibilityAreaId = responsibilityAreaId;
            condition.CaseCategoryId = 2;
            int totalCount = 0;
            List<CaseMainLine> list = CaseMainLineManager.LoadList(condition, 1, int.MaxValue, out totalCount);
            object[] caseList = (from l in list
                                 where l.CaseHappendTimeLower > startTime && l.CaseHappendTimeLower <= endTime
                                 select new
                                 {
                                     Id = l.Id,
                                     Code = l.Code,
                                     CaseType = l.CaseType.Name,
                                     ReportCaseTime = l.ReportCaseTime.ToString(),
                                     HappendArea = l.ChaCity.Name + l.ChaStreet.Name + l.ChaDetail,
                                     IsCallBack = l.IsCallBack,
                                     Url = ""
                                 }).ToArray();
            return caseList.ToList();
        }

        private List<object> getSecurityCase(int? responsibilityAreaId, DateTime? startTime, DateTime? endTime)
        {
            CaseMainLineCondition condition = new CaseMainLineCondition();
            condition.ResponsibilityAreaId = responsibilityAreaId;
            condition.CaseCategoryId = 4;
            int totalCount = 0;
            List<CaseMainLine> list = CaseMainLineManager.LoadList(condition, 1, int.MaxValue, out totalCount);
            object[] caseList = (from l in list
                                 where l.CaseHappendTimeLower > startTime && l.CaseHappendTimeLower <= endTime
                                 select new
                                {
                                    Id = l.Id,
                                    Code = l.Code,
                                    CaseType = l.CaseType.Name,
                                    ReportCaseTime = l.ReportCaseTime.ToString(),
                                    HappendArea = l.ChaCity.Name + l.ChaStreet.Name + l.ChaDetail,
                                    IsCallBack = l.IsCallBack,
                                    Url = ""
                                }).ToArray();
            return caseList.ToList();
        }

        private DateTime? getWeekStartTime()
        {
            DateTime now = DateTime.Now;
            DayOfWeek weekDay = now.DayOfWeek;
            DateTime startTime = now.AddDays(-Convert.ToInt32(weekDay) + 1).Date;
            if (weekDay == 0)
                startTime = now.AddDays(-7 + 1).Date;

            return startTime;
        }
        protected string CaseType
        {
            get
            {
                return Request["CaseType"];
            }
        }
    }
}